<?php 
	class com_search extends component
	{
		public $option='com_search';
		public $cat_conditions = '';
		public $post_conditions = '';

		public function get_content()
		{
			$this->get_search_conditions();
			$this->create_breadcrumbs();
			$this->get_search_form();
			$this->get_search_result();
		}
		//Get search conditions
		private function get_search_conditions()
		{
			$searchword = $_GET['searchword'];
			if ($searchword == '')
				return;
			$searchword = str_replace('"','',$searchword);
			$words = explode(' ',$searchword);
			$searchphrase = $_GET['searchphrase'];
			$searchin = $_GET['areas'];
			$num = count($searchin);
			if ($num == 0 || $num == 2) {
				$in = 2;
			}
			else {
				if ($searchin[0] == 'categories')
					$in = 0;
				else 
					$in = 1;
			}
			if ($searchphrase == 'exact') {
				$s = $words[0];
				$len = count($words);
				for ($i = 1; $i < $len; $i++)
					$s = $s.' '.$words[$i];
				if ($in == 0) {
					$this->cat_conditions .= "c.category_name LIKE '%".$s."%'";
				}
				else if ($in == 1){
					$this->post_conditions .= "(p.title LIKE '%".$s."%' OR p.intro LIKE '%".$s."%' OR p.content LIKE '%".$s."%')";
				}
				else {
					$this->cat_conditions .= "c.category_name LIKE '%".$s."%'";
					$this->post_conditions .= "(p.title LIKE '%".$s."%' OR p.intro LIKE '%".$s."%' OR p.content LIKE '%".$s."%')";
				}
			}
			else {
				if ($searchphrase == 'all') {
					$s = 'AND ';
				}
				else {
					$s = 'OR ';
				}
				foreach ($words as $word) {
					if ($in == 0) {
						$this->cat_conditions .= "c.category_name LIKE '%".$word."%' ".$s;
					}
					else if ($in == 1){
						$this->post_conditions .= "(p.title LIKE '%".$word."%' OR p.intro LIKE '%".$word."%' OR p.content LIKE '%".$word."%') ".$s;
					}
					else {
						$this->cat_conditions .= "c.category_name LIKE '%".$word."%' ".$s;
						$this->post_conditions .= "(p.title LIKE '%".$word."%' OR p.intro LIKE '%".$word."%' OR p.content LIKE '%".$word."%') ".$s;
					}
				}
				if ($in !== 0) {
				$len = strlen($this->post_conditions) - strlen($s) - 1;
				$this->post_conditions = substr($this->post_conditions, 0, $len);
				}
				if ($in !== 1) {
					$len = strlen($this->cat_conditions) - strlen($s) - 1;
					$this->cat_conditions = substr($this->cat_conditions, 0, $len);
				}
			}
		}
		//Get results
		private function get_search_result()
		{
			$ids = array();
			$count = 0;
			if ($this->cat_conditions == '' && $this->post_conditions == '')
				return;
			if ($this->cat_conditions !== '') {
				$query = "SELECT p.id FROM posts as p LEFT JOIN categories as c ON p.cat_id=c.id 
					WHERE p.state=1 AND c.state=1 AND (".$this->cat_conditions.")";
				$result = mysql_query($query) or die(mysql_error());
				$numRow = mysql_num_rows($result);
				$count += $numRow;
				if ($numRow > 0)
					while ($row = mysql_fetch_array($result))
						$ids[] = $row;
				
			}
			if ($this->post_conditions !== '') {
				$query = "SELECT p.id FROM posts as p LEFT JOIN categories as c ON p.cat_id=c.id 
					WHERE p.state=1 AND c.state=1 AND (".$this->post_conditions.")";
				$result = mysql_query($query) or die(mysql_error());
				$numRow = mysql_num_rows($result);
				$count += $numRow;
				if ($numRow > 0)
					while ($row = mysql_fetch_array($result))
						$ids[] = $row;
			}
			$postIds = '';
			if ($count !== 0) {
				$postIds = '('.$ids[0][id];
				for ($i = 1; $i < $count; $i++) {
					$postIds = $postIds.','.$ids[$i][id];
				}
				$postIds = $postIds.')';
			}
			$this->show_search_result($count,$postIds);
		}
		//Create breadcrumbs
		private function create_breadcrumbs()
		{
?>
			<div class="breadcrumbs" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb">
				<a class="first" href="http://localhost/cms" itemprop="url"><span itemprop="title">Home</span></a>
				<a href="http://localhost/cms/index.php?option=com_search" itemprop="url"><span itemprop="title">Search</span></a>
			</div><!-- End .breadcrumbs-->
<?php
		}
		//Get search form
		private function get_search_form()
		{
?>
			<div id="search-form-wrapper">
				<h1>Search</h1>
				<form method="get" action="index.php" name="search">
					<input type="hidden" name="option" value="com_search" />
					<input type="text" name="searchword" maxlength="20" class="inputbox" id="inputbox" value="<?php echo $_GET['searchword'];?>" />
					<input type="submit" value="Search" class="button" />
				
					<div class="phrases">
						<label for="phrases-text">Search for:</label>
						<input type="radio" name="searchphrase" value="all" checked />
						<label for="allphrase">All words</label>
						<input type="radio" name="searchphrase" value="any" />
						<label for="allphrase">Any words</label>
						<input type="radio" name="searchphrase" value="exact" />
						<label for="allphrase">Exact phrase</label>
					</div>
					<div class="only">
						<label for="only-text">Search only:</label>
						<input type="checkbox" name="areas[]" value="categories" />
						<label for="categories">Categories</label>
						<input type="checkbox" name="areas[]" value="articles" />
						<label for="categories">Articles</label>
					</div>
				</form>
			</div><!-- End #search-form-wrapper-->
<?php
		}
		//Show search result
		private function show_search_result($count,$postIds)
		{

?>
			<div id="search-result">
<?php
				if ($count == 0) {
					echo '<h2>Total: 0 results found.</h2></div>';
					return;
				}
				$query = "SELECT p.id,p.intro,p.title FROM posts as p WHERE p.id in $postIds";
				$result = mysql_query($query) or die(mysql_error());
				$numRow = mysql_num_rows($result);
				if ($numRow == 0) {
					echo '<h2>Total: 0 results found.</h2></div>';
					return;
				}
				if ($numRow == 1)
					echo '<h2>Total: 1 result found.</h2>';
				else
					echo '<h2>Total: '.$numRow.' results found.</h2>';
				while ($row = mysql_fetch_array($result)) {
					$postUrl = 'http://localhost/cms/index.php?option=com_content&view=article&id='.$row[id];
?>
					<div class="post-outer">
						<a href="<?php echo $postUrl;?>"><h2><?php echo $row[title];?></h2></a>
						<div class="post-content">
							<?php echo $row[intro];?>
						</div>
						<div class="jump-link">
							<a href="<?php echo $postUrl;?>">Read more &raquo;</a>
						</div>
					</div><!-- End .post-outer-->
<?php 
				}
?>
			</div><!-- End #search-result-->
<?php
		}
	}
?>
